package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAgricultural;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.ServiceInfoVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.viewholder.AgriculturalServiceListHolder;
import com.digitalchina.lxnsy.widget.EditTextWithClear;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：农事服务搜索
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2017/3/24 11:05:30
 * 修改备注：
 * ================================================
 */

public class AgriculturalServiceSearchActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.et_search)
    EditTextWithClear etSearch; // 搜索框

    public static final int RESULT_CODE_SERVICE_SEARCH = 2000;
    @Bind(R.id.agricultural_service_list_recycler_view_search)
    EasyRecyclerView recyclerView;

    private String keyWord = "";//搜索的关键字
    private int page = 1;
    private int limit = 10;
    private List<ServiceInfoVO> agriculturalList = new ArrayList<>();
    private RecyclerArrayAdapter adapter;


    @Override
    public void setLayout() {
        setTitle("农事服务搜索");
        setContentLayout(R.layout.activity_agricultural_service_search);
    }

    @Override
    public void initMember() {
        etSearch.setInputType(EditorInfo.TYPE_CLASS_TEXT); //软键盘 默认中文
        etSearch.setHint("请输入农事服务名称");
        initRecyclerView();
    }

    private void initRecyclerView() {


        recyclerView.setLayoutManager(new LinearLayoutManager(this));

//        recyclerView.addItemDecoration(Utils.setSpaceDecoration(this, false, true, false));

        recyclerView.setAdapter(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new AgriculturalServiceListHolder(parent);
            }
        });

    }

    @Override
    public void addListener() {
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int keyCode, KeyEvent keyEvent) {
                if (keyCode == EditorInfo.IME_ACTION_SEARCH) {
                    keyWord = StringUtil.getEditText(etSearch);
                    if (StringUtil.isStrEmpty(keyWord)) {
                        showToast("请输入关键字");
                    } else
                        onRefresh();
                    return true;
                }
                return false;
            }
        });

        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //点击农事服务列表查看农事服务详情
                ServiceInfoVO vo = (ServiceInfoVO) adapter.getItem(position);
//                String url = URL.SERVER + vo.getH5Url().substring(1);
                GotoUtil.gotoActivity(AgriculturalServiceSearchActivity.this, AgriculturalServiceDetailActivity.class, false, "toServiceDetails", vo);

            }
        });
    }

    /**
     * 返回关键字
     *
     * @param keyWord
     */
    private void setResultString(String keyWord) {
        Intent intent = new Intent();
        intent.putExtra("KeyWord", keyWord);
        setResult(RESULT_CODE_SERVICE_SEARCH, intent);
        finish();
    }

    @Override
    public void setHandler() {

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);
                switch (msg.what) {
                    case MSG.MSG_GET_AGRICULTURAL_SERVICE_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_AGRICULTURAL_SERVICE_LIST_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };

    }

    @Override
    public void callBack(Object object) {

        agriculturalList = FastJsonUtil.getListBean(object.toString(), "body", "serviceInfos", ServiceInfoVO.class);
        if (page == 1) { // 刷新
            adapter.clear();
        }

        if (agriculturalList != null) {
            if (agriculturalList.size() < limit) {
                adapter.stopMore();
            }
        }

        adapter.addAll(agriculturalList);


    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessAgricultural.getAgriculturalList(this, "", "", keyWord, String.valueOf(page), String.valueOf(limit), mHandler);
    }


    @Override
    public void onLoadMore() {
            page++;
            BusinessAgricultural.getAgriculturalList(this, "", "", keyWord, String.valueOf(page), String.valueOf(limit), mHandler);
    }
}
